//
//  REChangeViewController.m
//  录音
//
//  Created by ywy on 2016/12/27.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "REChangeViewController.h"
#import <UIImageView+WebCache.h>
#import "RECheckLogin.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "RETabBarViewController.h"

@interface REChangeViewController ()<UITextFieldDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property(nonatomic,strong) NSDictionary * userInfoDic;
@property(nonatomic,strong) UIImagePickerController*photo;

@end

@implementation REChangeViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
    //自定义隐藏Tabbar
//    tabBar.bottomView.hidden = NO;
//    tabBar.bottomView.alpha = 1;
//    tabBar.bottomView.backgroundColor = [UIColor redColor];
    [tabBar HideTabarView:YES];
    UIImageView * firstImageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"编辑资料"]];
    firstImageView.frame = CGRectMake(0, 0, 37, 17);
    self.navigationItem.titleView=firstImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicK)];
    [self.iconImageView addGestureRecognizer:tap];
    self.backView.layer.borderColor = RERGBColor(174, 174, 174).CGColor;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.iconStrUrl]]
                          placeholderImage:nil];
    self.nickTextField.delegate = self;
    self.nickTextField.text = self.nickeNameStr;
    if (![UserDefaults objectForKey:@"sex"]) {
        self.sexLabel.text =@"女";
    }else{
        self.sexLabel.text = [UserDefaults objectForKey:@"sex"];
    }
     [self getPersonalInfo];
}
-(void)getPersonalInfo
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary * pram = [NSMutableDictionary new];
    pram[@"cmd"] = @"getPersonalInfo";
    pram[@"uid"] = [UserDefaults objectForKey:@"userid"];
    [RECheckLogin POST:REBaseUrl parameters:pram progress:^(CGFloat prosser) {
        
    } success:^(id responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            weakSelf.userInfoDic = responseObject[@"userInfo"];
            [weakSelf.iconImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",   weakSelf.userInfoDic[@"userIcon"]]] placeholderImage:nil];
            weakSelf.nickTextField.text = weakSelf.userInfoDic[@"nickName"];
        }else
        {
            //            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败!! 查看网络!!"];
    }];
}

-(void)imageClicK
{
    // 有权限的时候 直接 跳转到相册选择相册中的图片
           self.photo=[[UIImagePickerController alloc]init];
            self.photo.delegate=self;
//            [photo.navigationBar setBackgroundImage:[UIImage imageNamed:@"tab_bg"] forBarMetrics:UIBarMetricsDefault];
            [self.photo.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
            [self.photo setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            self.photo.allowsEditing = YES;
//            [photo setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
            [self presentViewController:self.photo animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    self.iconImageView.image = image;
//    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
//    //自定义隐藏Tabbar
//    [tabBar HideTabarView:NO];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
//    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
//    //自定义隐藏Tabbar
//    [tabBar HideTabarView:NO];
      [picker dismissViewControllerAnimated:YES completion:nil];
}
// 保存
- (IBAction)saveClicK:(UIButton *)sender {
    
    self.photo = nil;
    self.photo.delegate = nil;
    
    NSMutableDictionary * parm = [NSMutableDictionary new];
    parm[@"cmd"] =  @"updateNickname";
    parm[@"uid"] = [UserDefaults objectForKey:@"userid"];
    parm[@"userIcon"] = [RECheckLogin imageBase64WithDataURL:self.iconImageView.image];
    
    
    parm[@"userName"] = self.nickTextField.text;
    [SVProgressHUD showWithStatus:@"修改昵称中....."];
    
    [RECheckLogin POST:REBaseUrl parameters:parm progress:^(CGFloat proser) {
        
        
    } success:^(id responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            
            if (_changeName) {
                _changeName(responseObject[@"userIcon"],responseObject[@"userName"]);
            }
            [SVProgressHUD showSuccessWithStatus:responseObject[@"resultNote"]];
            [UserDefaults setObject:self.sexLabel.text forKey:@"sex"];
            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
              [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
          [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
    }];
}

- (IBAction)manButtonClick:(UIButton *)sender {
    if ([self.sexLabel.text isEqualToString:@"男"]) {
         self.sexLabel.text = @"女";
    }else if ([self.sexLabel.text isEqualToString:@"女"]){
        self.sexLabel.text = @"男";
    }
    
}

- (IBAction)womanClick:(UIButton *)sender {
    if ([self.sexLabel.text isEqualToString:@"男"]) {
        self.sexLabel.text = @"女";
    }else if ([self.sexLabel.text isEqualToString:@"女"]){
        self.sexLabel.text = @"男";
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
//        //在这里做你响应return键的代码
//        [textView resignFirstResponder];
//        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
//    }
//    return YES;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
